<?php

use App\Enums\UserGenderEnum;
use App\Enums\WorkTicketStatusEnum;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use App\Enums\WorkTicketTransferStatusEnum;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 系统配置表
        Schema::create('sys_configs', function (Blueprint $table) {
            $table->comment('系统配置表');
            $table->id();
            $table->string('key')->comment('配置键');
            $table->string('value')->comment('配置值');
            $table->string('group')->nullable()->comment('配置组');
            $table->string('memo')->nullable()->comment('备注');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('系统配置表');
        });

        Schema::create('schools', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('学校名称');
            $table->string('address')->nullable()->comment('学校地址');
            $table->string('tel')->nullable()->comment('学校电话');
            $table->string('memo')->nullable()->comment('备注');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('学校表');
        }); 

        // 用户表
        Schema::create('school_users', function (Blueprint $table) {
            $table->id();
            $table->integer('school_id')->comment('学校id');
            $table->string('real_name')->comment('真实姓名');
            $table->string('account')->nullable()->comment('账号');
            $table->string('tel')->nullable()->comment('手机号');
            $table->string('id_card')->nullable()->comment('身份证号');
            $table->boolean('enable')->default(true)->comment('是否启用');
            $table->string('memo')->nullable()->comment('备注');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('学校用户表');
        });

        // 维护人员表
        Schema::create('work_users', function (Blueprint $table) {
            $table->id();
            $table->string('real_name')->comment('真实姓名');
            $table->string('username')->nullable()->comment('用户名');
            $table->string('tel')->nullable()->comment('手机号');
            $table->boolean('enable')->default(true)->comment('是否启用');
            $table->string('memo')->nullable()->comment('备注');
            $table->string('token')->nullable()->comment('token');
            $table->timestamp('expired_at')->nullable()->comment('过期时间');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('维护人员表');
        });

        // 维护人员学校关联表
        Schema::create('work_user_schools', function (Blueprint $table) {
            $table->id();
            $table->integer('work_user_id')->comment('维护人员id');
            $table->integer('school_id')->comment('学校id');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('维护人员学校关联表');
        });

        // 工单表
        Schema::create('work_tickets', function (Blueprint $table) {
            $table->id();
            $table->string('code')->comment('工单编号');
            $table->integer('school_id')->comment('学校id');
            $table->integer('school_user_id')->comment('学校用户id');
            $table->string('room_number')->comment('房间号');
            $table->string('contact_name')->comment('联系人');
            $table->string('contact_tel')->comment('联系人电话');
            $table->text('content')->comment('工单内容');
            $table->integer('work_user_id')->nullable()->comment('维护人员id');
            $table->tinyInteger('status')->default(WorkTicketStatusEnum::待处理)->comment('状态|100：待处理,101：处理中,102：已处理,103：已取消');
            $table->timestamp('finish_at')->nullable()->comment('完成时间');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('工单表');
        });


        // 工单详情表
        Schema::create('work_ticket_details', function (Blueprint $table) {
            $table->id();
            $table->integer('work_ticket_id')->comment('工单id');
            $table->integer('related_id')->comment('关联id');
            $table->string('related_type')->comment('关联类型,关联表的表名');
            $table->text('content')->comment('内容');
            $table->timestamps();
            $table->softDeletes();
            $table->comment('工单详情表');
        });

        // 工单图片表
        Schema::create('work_ticket_images', function (Blueprint $table) {
            $table->id();
            $table->integer('work_ticket_id')->comment('工单id');
            $table->integer('work_ticket_detail_id')->comment('工单详情id');
            $table->string('path')->comment('图片路径');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('工单图片表');
        });

        // 工单转接表
        Schema::create('work_ticket_transfers', function (Blueprint $table) {
            $table->id();
            $table->integer('work_ticket_id')->comment('工单id');
            $table->integer('from_work_user_id')->comment('转接前维护人员id');
            $table->integer('to_work_user_id')->comment('转接后维护人员id');
            $table->string('remark')->nullable()->comment('转接备注');
            $table->tinyInteger('status')->default(WorkTicketTransferStatusEnum::待接受)->comment('状态|100:待接受,101:已接受,102:已拒绝');
            $table->timestamp('accept_at')->nullable()->comment('接受时间');
            $table->timestamp('reject_at')->nullable()->comment('拒绝时间');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('工单转接表');
        });

        // 工单评价表
        Schema::create('work_ticket_rating', function (Blueprint $table) {
            $table->id();
            $table->integer('work_ticket_id')->comment('工单id');
            $table->integer('work_user_id')->nullable()->comment('维护人员id');
            $table->integer('school_user_id')->nullable()->comment('学校用户id');
            $table->tinyInteger('score')->comment('评分|100:满意,101:一般,102:不满意');
            $table->text('content')->comment('评价内容');
            $table->timestamps();
            $table->softDeletes();

            $table->comment('工单评价表');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('sys_configs');
        Schema::dropIfExists('schools');
        Schema::dropIfExists('school_users');
        Schema::dropIfExists('work_users');
        Schema::dropIfExists('work_user_schools');
        Schema::dropIfExists('work_tickets');
        Schema::dropIfExists('work_ticket_details');
        Schema::dropIfExists('work_ticket_images');
        Schema::dropIfExists('work_ticket_transfers');
        Schema::dropIfExists('work_ticket_rating');
    }
};
